#!/bin/bash
#pMusic - external menu
#GPL - Copyright Sigmund Berglund

export TEXTDOMAIN=pmusic
export OUTPUT_CHARSET=UTF-8
SF='space-expand="false" space-fill="false"'
ST='space-expand="true" space-fill="true"'
TAB="$1"

template(){
	echo '
	<eventbox above-child="true" visible-window="true">
	 <hbox spacing="7" border-width="3">
	  <pixmap '$SF'><height>18</height><input file>'$APPDIR'/themes/'$THEME'/'$3'</input></pixmap>
	  <text xalign="0" '$ST'>
	   <label>'$1'</label>
	  </text>
	 </hbox>
	 <action signal="enter-notify-event">hide:'$2'</action>
	 <action signal="enter-notify-event">show:'$2'B</action>
	 <variable>'$2'</variable>
	</eventbox>
	<eventbox name="rightclick_menu" above-child="true" visible-window="true" visible="false">
	 <hbox spacing="7" border-width="3">
	  <pixmap '$SF'><height>18</height><input file>'$APPDIR'/themes/'$THEME'/'$3'</input></pixmap>
	  <text xalign="0" '$ST'>
	   <label>'$1'</label>
	  </text>
	 </hbox>
	 <action signal="leave-notify-event">show:'$2'</action>
	 <action signal="leave-notify-event">hide:'$2'B</action>
	 <action signal="button-release-event">EXIT:'$2'</action>
	 <variable>'$2'B</variable>
	</eventbox>
	'
}

export gtkdialog_menu='
<window decorated="false" border-width="0" skip_taskbar_hint="true" resizable="false" modal="true" '$ST'>
 <eventbox name="rightclick_menu_frame" above-child="false">
  <vbox border-width="1">
   <eventbox>
    <vbox spacing="0" border-width="7">
     <hbox spacing="2">
      <entry>
       <variable>PLAYLIST_NAME</variable>
       <input file>'$WORKDIR'/PLAYLIST_'${TAB}'_NAME</input>
       <output file>'$WORKDIR'/PLAYLIST_'${TAB}'_NAME</output>
       <action signal="activate">save:PLAYLIST_NAME</action>
       <action signal="activate">EXIT:rename</action>
      </entry>
     </hbox>
     <text height-request="10"><label>""</label></text>
     '"
     <hseparator></hseparator>
     $(template " $(gettext 'Delete Queue')" delete_queue icon_pmusic_remove.svg)
     "'
    </vbox>
   </eventbox>
  </vbox>
 </eventbox>
 <action signal="focus-out-event">EXIT:exit</action>
</window>'

I=$IFS; IFS=""
for STATEMENTS in $($GTKDIALOG -p gtkdialog_menu -G +"$(($PTR_X_ROOT-150))"+"$(($PTR_Y_ROOT-5))"); do
	eval $STATEMENTS
done
IFS=$I

case "$EXIT" in
	delete_queue)
		$APPDIR/func_store_playlist
		rm -f $WORKDIR/PLAYLIST_${TAB}_NAME
		#rebuild tablist after a tab has been removed
		for I in 1 2 3 3 4 5 6 7 8 9; do
			read NAME < $WORKDIR/PLAYLIST_$((${I}+1))_NAME
			if [[ ! -f $WORKDIR/PLAYLIST_${I}_NAME && "$NAME" ]]; then
				mv -f $WORKDIR/playlist_$((${I}+1)) $WORKDIR/playlist_${I}
				mv $WORKDIR/PLAYLIST_$((${I}+1))_NAME $WORKDIR/PLAYLIST_${I}_NAME
				mv -f $WORKDIR/PLAYLIST_$((${I}+1))_ICON.svg $WORKDIR/PLAYLIST_${I}_ICON.svg
				
			fi
		done
		#switch to tab 1 in case the deleted tab is the active one
		read TMP < $WORKDIR/PLAYLIST_X
		if [  $TMP = $TAB ]; then
			echo 1 > PLAYLIST_X
			cp -f $APPDIR/themes/$THEME/icon_playqueue_marker.svg $WORKDIR/PLAYLIST_1_ICON.svg
			cp -f $WORKDIR/playlist_1 $WORKDIR/playlist
		fi
		#---
		rm -f $WORKDIR/xml_pmusic #update gui to add visible="true" for removed tab
		;;
esac
